<template>
    <scroller class="page">
        <div class="cart-list">
            <div v-for="(item, index) in list">
                <div class="cart-item">
                    <div class="cart-item-checkbox">
                        <wxc-checkbox :value="index" :checked="item.checked" :has-bottom-border="false" @wxcCheckBoxItemChecked="wxcCheckBoxItemChecked"></wxc-checkbox>
                    </div>
                    <image class="cart-item-img" resize="cover" :src="item.cover"></image>
                    <div class="cart-item-left">
                        <text class="cart-item-title">{{ item.title }}（{{ item.formatTitle }}）</text>
                        <text class="cart-item-subtitle">{{ item.subtitle }}</text>
                    </div>
                    <div class="cart-item-right">
                        <text class="cart-item-price">￥{{ item.formatPrice }}</text>
                        <text class="cart-item-count">x{{ item.count }}</text>
                    </div>
                </div>
            </div>
            <div class="cart-list-count">
                <text class="cart-list-count-text">共 {{ list.length }} 件商品</text>
            </div>
        </div>
        <Footer />
    <!-- <div class="top">
      <div class="title">
        <text class="title-text">购物车</text>
        <div class="underline"></div>
      </div>
    </div> -->
        <div class="bottom">
            <div class="bottom-left">
                <div class="bottom-left-check-all">
                    <wxc-checkbox class="bottom-left-check-all-img" :has-bottom-border="false" @wxcCheckBoxItemChecked="allChecked"></wxc-checkbox>
                    <text class="bottom-left-check-all-text">全选</text>
                </div>
                <div class="bottom-left-sum">
                    <text class="bottom-left-sum-text-1">合计：</text>
                    <text class="bottom-left-sum-text-2">￥{{ sum }}</text>
                </div>
            </div>
            <div class="bottom-right" @click="goPay">
                <text class="bottom-right-text">去支付</text>
            </div>
        </div>
    </scroller>
</template>
<script>
import Footer from '@/components/Footer';
import { WxcCheckbox } from 'weex-ui';
import utils from './utils/utils.js';
const modal = weex.requireModule('modal');
const authChannel = new BroadcastChannel('auth');
export default {
    name: 'App',
    components: {
        WxcCheckbox,
        Footer
    },
    data() {
        return {
            list: [],
            sum: 0
        }
    },
    mounted() {
        var self = this
        var event = weex.requireModule('event');
        event.initData(JSON.stringify({ 'title': '购物车' }), json => {
            if (utils.checkToken()) {
                self.init();
                console.log('cart login suc1')
            } else {
                console.log('cart login fail1')
            }
        })
        event.initLife("index", function(json) {
            console.log('initLife')
            if (json == 'viewDidAppear') {
                console.log('vda')
                if (utils.checkToken()) {
                    console.log('u console')
                    this.init();
                    console.log('cart login suc')
                } else {
                    console.log('cart login fail')
                }
            }
        })

        // 收到成功登录信息时刷新页面
        authChannel.onmessage = event => {
            self.init();
        }
    },
    methods: {
        init() {
            var url = '/yq/cart/list';
            var params = {};
            var success = res => {
                console.log(res);
                this.list = res.info;
                // for (var i = 0; i < this.list.length; i++) {
                //   this.list[i].checked = false;
                // }
            }
            var fail = res => {
                modal.toast({
                    message: '获取数据失败',
                    duration: 1
                })
            }
            utils.requestAuth(url, params, success, fail);
        },
        wxcCheckBoxItemChecked(e) {
            var index = e.value;
            this.list[index].checked = e.checked;
            this.countSummary();
        },
        allChecked(e) {
            for (var i = 0; i < this.list.length; i++) {
                this.list[i].checked = e.checked;
            }
            this.countSummary();
        },
        countSummary() {
            var sum = 0;
            for (var i = 0; i < this.list.length; i++) {
                if (this.list[i].checked) {
                    sum += this.list[i].formatPrice;
                }
            }
            this.sum = sum;
        },
        goPay() {
            var listToPay = new Array;
            for (var i = 0; i < this.list.length; i++) {
                if (this.list[i].checked) {
                    listToPay.push(this.list[i]);
                }
            }
            if (listToPay.length == 0) {
                modal.toast({
                    message: '请选择至少一件商品',
                    duration: 2
                })
                return;
            }
            var event = weex.requireModule('event');
            var params = {
                path: 'confirm',
                data: JSON.stringify({
                    list: listToPay,
                    isFromCart: true
                })
            }
            event.onPush(JSON.stringify(params));
        }
    }
}
</script>
<style scoped>
text {
    font-size: 32px;
}

.page {
    padding-bottom: 150px;
}

.top {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 110px;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    text-align: center;
    border-bottom-width: 1px;
    border-bottom-style: solid;
    border-bottom-color: #aaa;
    background-color: #fff;
}

.title {
    flex-direction: column;
    align-items: center;
}

.title-text {
    font-weight: bold;
    margin-bottom: 10px;
}

.underline {
    height: 3px;
    width: 100px;
    background-color: #000;
}

.cart-list {
    /*padding-top: 110px;*/
    padding-top: 30px;
}

.cart-item {
    padding: 30px 30px 30px 0;
    flex-direction: row;
}

.cart-item-checkbox {
    width: 100px;
}

.cart-item-img {
    width: 120px;
    height: 120px;
}

.cart-item-left {
    width: 370px;
    padding: 0 30px;
}

.cart-item-title {
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 20px;
}

.cart-item-subtitle {
    font-size: 24px;
    color: #888;
}

.cart-item-right {
    width: 130px;
    align-items: flex-end;
}

.cart-item-price {
    font-size: 28px;
    font-weight: bold;
    margin-bottom: 30px;
}

.cart-item-count {
    font-size: 24px;
    color: #888;
}

.cart-list-count {
    padding: 30px 30px 0 0;
    flex-direction: row;
    justify-content: flex-end;
}

.cart-list-count-text {
    font-weight: bold;
}

.bottom {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #fff;
    border-top-width: 1px;
    border-top-style: solid;
    border-top-color: #aaa;
    flex-direction: row;
    justify-content: space-between;
    align-items: stretch;
    height: 110px;
}

.bottom-left {
    width: 400px;
    flex-direction: row;
    align-items: center;
}

.bottom-left-check-all {
    flex-direction: row;
    align-items: center;
    width: 150px;
    padding-left: 30px;
}

.bottom-left-check-all-img {
    padding-left: 0 !important;
    padding-right: 0 !important;
    margin-right: 10px;
}

.bottom-left-check-all-text {
    font-size: 28px;
    color: #527a5f;
}

.bottom-left-sum {
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    width: 250px;
    padding-right: 20px;
}

.bottom-left-sum-text-1 {
    font-size: 30px;
}

.bottom-left-sum-text-2 {
    font-size: 30px;
    color: #527a5f;
    font-weight: bold;
}

.bottom-right {
    width: 350px;
    justify-content: center;
    align-items: center;
    text-align: center;
    background-color: #527a5f;
}

.bottom-right-text {
    font-size: 34px;
    color: #fff;
}
</style>